<template>
  <div id="propPieChart"></div>
</template>
<script setup>
import * as echarts from 'echarts'
import { getDifferentConut } from '@/api/activeFault'
import imgUrl from '@/assets/images/activeFault/age.png'

const createEcharts = (color, data) => {
  let myChart = echarts.init(document.getElementById("propPieChart"));
  myChart && myChart.setOption({
    tooltip: {
      trigger: 'item'
    },
    grid: {
      left: '0%',
      right: '0%',
      bottom: '0%',
      containLabel: false // 包含坐标轴标签在内
    },
    legend: {
      orient: 'vertical',  // 设置图例垂直排列
      right: '10px',         // 设置图例在右侧
      top: 'center',        // 设置图例在垂直方向上居中
      textStyle: {
        color: "#fff",
        fontSize: 16,
        padding: [0, 4]//文字与图形之间的左右间距
      },
      itemWidth: 10,    // 设置图例项的宽度为10像素
      itemHeight: 10    // 设置图例项的高度为10像素
    },
    color: color, // 设置饼状图的颜色
    graphic: {
      elements: [
        {
          type: 'image',
          style: {
            image: imgUrl,
            width: 90,
            height: 90,
          },
          left: '25%',
          top: 'center'
        }
      ]
    },
    series: [
      {
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['35%', '50%'],
        labelLine: {
          show: false
        },
        data: data,
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#000947',
          borderWidth: 6
        },
        label: {
          show: true,
          color: '#46FFFF',
          formatter: '{c}%',
          fontSize: 16
        }
      }
    ]
  });
}

let color = ['#EF862B', '#46FFFF', '#A6D839', '#4555F0']
const type = [
  { value: '0', name: '全新世' },
  { value: '1', name: '晚更新世' },
  { value: '2', name: '早中更新世' },
  { value: '3', name: '前第四纪' }
]
const data = ref([])
const differentConut = () => {
  getDifferentConut().then(res => {
    // activityEraType   活动时代类型  0全新世  1晚更新世  2早中更新世  3前第四纪
    data.value = res.data.map(item => {
      return {
        name: type.find(item1 => item.activityEraType == item1.value)?.name,
        value: item.activityEraProportion
      }
    })
    createEcharts(color, data.value)
  })
}
onMounted(() => {
  differentConut()
  window.addEventListener('resize', resize)
})
const resize = () => {
  let myChart = echarts.init(document.getElementById("numPieChart"));
  myChart.resize()
}
onBeforeUnmount(() => {
  window.removeEventListener('resize', resize)
})
</script>
<style scoped lang="scss">
#propPieChart {
  width: 450px;
  font-size: 14px;
  background: url('@/assets/images/activeFault/age-bg.png') no-repeat;
  background-position: 15% 100%;
  background-size: 55% 100%;
  margin: 10px 0;
  height: calc(100% - 70px);
  min-height: 125px;
  &>div {
    height: 100%;
  }
}
</style>